<template>
  <div id="app">
    <!-- 布局容器 -->
    <el-container>
      <!-- 头部组件 -->
      <el-header>
        <el-menu default-active="1" mode="horizontal" @select="handleSelect">
          <el-menu-item index="1">最新壁纸</el-menu-item>
          <el-submenu index="2">
            <template slot="title">壁纸分类</template>
            <el-menu-item
              :index="item.id"
              v-for="(item, index) in BiZhiClassification"
              :key="index"
            >{{ item.name }}</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-header>
      <!-- 中间组件 -->
      <el-main>
        <lastest v-show="lastestIsSHowFlag"></lastest>
        <classification v-if="!lastestIsSHowFlag" :id="cid"></classification>
      </el-main>
    </el-container>

    <template>
      <el-backtop></el-backtop>
    </template>
  </div>
</template>

<script>
import {
  getTheLatestBiZhi,
  getBiZhiClassification,
  getPictureByBiZhiClassification,
} from "@/api/index.js";

import lastest from "./components/lastestBiZhi.vue";
import classification from "./components/classificationBiZhi.vue";
export default {
  components: {
    lastest,
    classification,
  },
  mounted() {},
  created() {
    this.funGetBiZhiClassification();
  },
  data() {
    return {
      /**
       * 是否显示最新壁纸
       */
      lastestIsSHowFlag: true,
      /**
       * 壁纸列表
       */
      BiZhiList: [],
      /**
       * 壁纸分类
       */
      BiZhiClassification: [],
      /**
       * 壁纸分类
       */
      cid: null,
      /**
       * 开始壁纸
       */
      start: 0,
      /**
       * 壁纸数量
       */
      count: 60,
    };
  },
  methods: {
    /**
     * 获取壁纸分类
     */
    async funGetBiZhiClassification() {
      await getBiZhiClassification().then(
        (res) => {
          // console.log(res.data.data);
          this.BiZhiClassification = res.data.data;
        },
        (err) => {
          // for debug
          console.log(err);
        }
      );
    },
    /**
     * 通过 cid 获取响应分类的壁纸
     */
    async funGetPictureByBiZhiClassification(cid, start = 0, count = 50) {
      this.BiZhiList = [];
      await getPictureByBiZhiClassification(cid, start, count).then(
        (res) => {
          // console.log(res);
          // console.log(res.data.data);
          let temp = res.data.data;
          temp.map((item, index) => {
            let tempData = {
              id: item.id,
              title: item.utag,
              time: item.create_time,
              imageUrl: item.img_1600_900,
            };
            this.BiZhiList.push(tempData);
          });
        },
        (err) => {
          // for debug
          console.log(err);
        }
      );
    },
    /**
     * 顶部导航栏被点击
     */
    handleSelect(key, keyPath) {
      // console.log(key, keyPath);
      if (key == 1) {
        // key == 1 表示点击了 "最新壁纸" 顶部导航栏
        this.lastestIsSHowFlag = true;
      } else {
        this.cid = key;
        this.funGetPictureByBiZhiClassification(key, this.start, this.count);
        this.lastestIsSHowFlag = false;
      }
    },
    theNext() {
      this.start += 10;
      this.count += 10;
      this.funGetPictureByBiZhiClassification(this.cid, this.start, this.count);
    },
  },
};
</script>

<style>
.el-header {
  padding: 0;
  position: fixed;
  top: 0;
  width: 100%;
  line-height: 60px;
  z-index: 300;
}
.el-row {
  margin-top: 50px;
}
.el-col {
  padding-top: 10px;
  padding-bottom: 10px;
}
.el-pagination {
  width: 100%;
  background-color: #ffffff;
  position: fixed;
  bottom: 0;
  display: flex;
  justify-content: center;
}
</style>
